using System;
using System.Collections;
using System.Collections.Generic;
using MFarm.Dialogue;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class DialogueUI : MonoBehaviour
{
    public GameObject player;
    public GameObject dialogueBox;
    public CanvasGroup dialoguePanel;
    public Text dailogueText;
    public Image faceRight, faceLeft;
    public Text nameRight, nameLeft;
    public GameObject continueBox;

    private void Awake()
    {
        player= GameObject.FindWithTag("Player");
    }

    private void Start()
    {
        dialoguePanel.alpha = 0;
    }

    private void OnEnable()
    {
        EventHandler.ShowDialogueEvent += OnShowDailogueEvent;
    }

    private void OnDisable()
    {
        EventHandler.ShowDialogueEvent -= OnShowDailogueEvent;
    }

    private void OnShowDailogueEvent(DialoguePiece piece)
    {
        StartCoroutine(ShowDialogue(piece));
    }

    private IEnumerator ShowDialogue(DialoguePiece piece)
    {
        if (piece != null)
        {
            piece.isDone = false;
            dialoguePanel.alpha = 1;
            dialogueBox.SetActive(true);
           // continueBox.SetActive(false);

            dailogueText.text = string.Empty;

            if (piece.name != string.Empty)
            {
                if (piece.onLeft)
                {
                    //faceRight.gameObject.SetActive(false);
                    //faceLeft.gameObject.SetActive(true);
                    //faceLeft.sprite = piece.faceImage;
                    //nameLeft.text = piece.name;
                }
                else
                {
                    //faceRight.gameObject.SetActive(true);
                    //faceLeft.gameObject.SetActive(false);
                    //faceRight.sprite = piece.faceImage;
                    //nameRight.text = piece.name;
                }
            }
            else
            {
               // faceLeft.gameObject.SetActive(false);
                //faceRight.gameObject.SetActive(false);
               // nameLeft.gameObject.SetActive(false);
               // nameRight.gameObject.SetActive(false);
            }
            yield return dailogueText.DOText(piece.dialogueText, 1f).WaitForCompletion();

            piece.isDone = true;

           // if (piece.hasToPause && piece.isDone)
                //continueBox.SetActive(true);
        }
        else
        {
            dialogueBox.SetActive(false);
            dialoguePanel.alpha = 0;
            yield break;
        }
    }

    private void Update()
    {
        dialoguePanel.transform.localScale= player.transform.localScale;
    }
}
